package com.teach;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Random;
import java.util.function.Consumer;
import java.util.function.Predicate;

import com.teach.model.Student1;
import com.teach.model.Student2;

public class Demo02Collection {

	public static void main(String[] args) {
		// Collection<E> //类<E>,泛型类, 这里表示集合的内部的元素的具体类型
//		int[] intArray;
//		Collection<int>//编译错误,泛型类型只能是引用类型
		Collection<Integer> c1 = new ArrayList<Integer>();// 接口的实例化必须使用实现的子类
		System.out.println("c1=" + c1);
		c1.add(1);
		c1.add(2);
		System.out.println("c1=" + c1);
		Collection<Integer> c2 = new HashSet<Integer>();
		System.out.println("c2=" + c2);
		System.out.println("将集合c1添加到c2中:" + c2.addAll(c1));
		System.out.println("c2=" + c2);
		System.out.println("向c2中添加1:" + c2.add(1));
		System.out.println("向c2中添加3:" + c2.add(3));
		System.out.println("c2=" + c2);
		System.out.println("清空c2");
		c2.clear();
		System.out.println("c2=" + c2);
		System.out.println("向c1中随机加入10个1-20的数");
		Random r = new Random();
		for (int i = 0; i < 10; i++) {
			c1.add(r.nextInt(20)+1);
		}
		System.out.println("c1=" + c1);
		System.out.println("集合是否包含1:"+c1.contains(1));
		System.out.println("集合是否包含-1:"+c1.contains(-1));
		System.out.println("集合是否包含10:"+c1.contains(10));
		Collection<Integer> c3 = Arrays.asList(new Integer[] {1,2});//asList的方法企业中禁止使用
		System.out.println("集合是否包含[1,2]:"+c1.containsAll(c3));
		c3 = Arrays.asList(new Integer[] {1,-2});
		System.out.println("集合是否包含[1,-2]:"+c1.containsAll(c3));
		c3 = Arrays.asList(new Integer[] {1,10});
		System.out.println("集合是否包含[1,10]:"+c1.containsAll(c3));
		c3 = null;
		System.out.println("c1是否空?"+c1.isEmpty());
		System.out.println("c2是否空?"+c2.isEmpty());
//		System.out.println("c3是否空?"+c3.isEmpty());//NullPointerException
		c1.add(1);
		System.out.println("c1=" + c1);
		System.out.println("c1中移除1:"+c1.remove(new Integer(1)));
		System.out.println("c1=" + c1);
		System.out.println("c1中移除1:"+c1.remove(new Integer(1)));
		System.out.println("c1=" + c1);
		System.out.println("c1中移除1:"+c1.remove(new Integer(1)));
		System.out.println("c1=" + c1);
		
		Collection<Student1> c4 = new ArrayList<Student1>();
		Student1 s1 = new Student1(1L, "张三");
		c4.add(s1);
		c4.add(new Student1(1L, "张三"));
		c4.add(s1);
		c4.add(new Student1(2L, "李四"));
		c4.add(new Student1(2L, "李四"));
		System.out.println("c4=" + c4);
		System.out.println("c4中是否包含新对象张三:"+c4.contains(new Student1(1L, "张三")));
		System.out.println("c4中是否包含对象s1张三:"+c4.contains(s1));
		System.out.println("c4中删除新对象张三:"+c4.remove(new Student1(1L, "张三")));
		System.out.println("c4中删除新对象s1张三:"+c4.remove(s1));
		System.out.println("c4=" + c4);
		

		
		Collection<Student2> c5 = new ArrayList<Student2>();
		Student2 s2 = new Student2(1L, "张三");
		c5.add(s2);
		c5.add(new Student2(1L, "张三"));
		c5.add(s2);
		c5.add(new Student2(2L, "李四"));
		c5.add(new Student2(2L, "李四"));
		System.out.println("c5=" + c5);
		System.out.println("c5中是否包含新对象张三:"+c5.contains(new Student2(1L, "张三")));
		System.out.println("c5中是否包含对象s2张三:"+c5.contains(s2));
		System.out.println("c5中删除新对象张三:"+c5.remove(new Student2(1L, "张三")));
		System.out.println("c5中删除新对象s2张三:"+c5.remove(s2));
		System.out.println("c5=" + c5);
		
		//结论:集合的contains方法和remove[removeAll]方法中是使用equals方法判断两个对象是否一致的

		System.out.println("c1=" + c1);
		System.out.println("c1.size()="+c1.size());
		
		Object[] os = c1.toArray();
		System.out.println("os="+Arrays.toString(os));
		Integer[] is = c1.toArray(new Integer[0]);
		System.out.println("is="+Arrays.toString(is));
//		int[] is1 = c1.toArray(new int[0]);//不能直接转成对应的基本数据类型数组
		
		//条件删除
		c1.removeIf(new Predicate<Integer>() {

			@Override
			public boolean test(Integer t) {
				return t%2==0;
			}
			
		});
		c1.removeIf(t -> t%2==0);//lambda
		System.out.println("c1=" + c1);
		
		System.out.println("使用增强型for循环");
		for(Integer item : c1) {
			System.out.println(item);
		}
		//增强型for循环:数组,集合,实现了java.lang.Iterable的类
		System.out.println("使用迭代器遍历");
		Iterator<Integer> it = c1.iterator();
		while(it.hasNext()) {
			Integer item = it.next();
			System.out.println(item);
		}
		System.out.println("forEach方法");
		c1.forEach(new  Consumer<Integer>() {

			@Override
			public void accept(Integer t) {
				System.out.println(t);
			}
			
		});
		System.out.println("-------------------");
		c1.forEach((t)->{System.out.println(t);});//lambda
		
//		c1.stream();
		//jdk1.8+ 函数式编程 = lambda+stream流式
		
	}
}
